package gov.pbc.gx.nn.pz.dao;

import gov.pbc.gx.nn.pz.entity.Account;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

@Component
@Repository
public class QueryAccountDao extends AbstractJdbcDao {
	// 获取数据库全部数据
	public void getAll(Page<Account> page, Criteria criteria) throws Exception {
		String sql = "SELECT\n"
				+ "	A.*\n"
				+ "FROM\n"
				+ "	hc_account A\n"
				+ "INNER JOIN bdf2_user_dept UD1 ON UD1.USERNAME_ = A.CREATE_USER_\n"
				+ "INNER JOIN bdf2_user_dept UD2 ON UD1.DEPT_ID_=UD2.DEPT_ID_\n"
				+ "WHERE UD2.USERNAME_=:username and A.INVALID_=0";

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", ContextHolder.getLoginUserName());
		ParseResult pr = this.parseCriteria(criteria, true, "A");
		if (pr != null) {
			StringBuffer sbSql = pr.getAssemblySql();
			sql += " and " + sbSql.toString();
			params.putAll(pr.getValueMap());
		}
		this.pagingQuery(page, sql, new RowMapper<Account>() {

			@Override
			public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
				Account a = new Account();
				a.setId(rs.getString("ID_"));
				a.setAccount(rs.getString("ACCOUNT_"));
				a.setBank(rs.getString("BANK_"));
				a.setCreateUser(rs.getString("CREATE_USER_"));
				a.setCreateDate(rs.getDate("CREATE_DATE_"));
				a.setUpdateDate(rs.getDate("UPDATE_DATE_"));
				a.setUpdateUser(rs.getString("UPDATE_USER_"));
				a.setInvalid(rs.getBoolean("INVALID_"));
				a.setName(rs.getString("NAME_"));
				return a;
			}
		}, params);
	}

}